package org.example.taskmaster.activities;

import org.example.taskmaster.R;
import org.example.taskmaster.fragments.DetailsFragment;
import org.example.taskmaster.fragments.DetailsFragmentFactory;

import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// register context with DetailsFragmentFactory, for calls to Toast
		DetailsFragmentFactory.setContext(this);
		
        // sets fragment_layout.xml to describe the layout for this page
		setContentView(R.layout.fragment_layout);
		
		// initially, ListSelect is shown in details area
		loadDetailFragment(DetailsFragmentFactory.UPCOMING_TASKS);
	}
	
	// Methods for loading content into the details area
	public void showAllTasks() {
		DetailsFragment detail = DetailsFragmentFactory.getDetailsFragment(DetailsFragmentFactory.ALL_TASKS);
		loadDetailFragment(detail);
	}
	public void showTaskDetail(Long rowId) {
		DetailsFragment detail = DetailsFragmentFactory.getDetailsFragment(DetailsFragmentFactory.TASK_DETAIL, rowId);
		loadDetailFragment(detail);
	}
	public void showTaskList(String list) {
		DetailsFragment detail = DetailsFragmentFactory.getDetailsFragment(DetailsFragmentFactory.TASK_LIST, list);
		loadDetailFragment(detail);
	}
	public void showListSelect() {
		DetailsFragment detail = DetailsFragmentFactory.getDetailsFragment(DetailsFragmentFactory.SELECT_LIST);
		loadDetailFragment(detail);
	} 
	public void showEditTask(Long rowId) {
		DetailsFragment detail = DetailsFragmentFactory.getDetailsFragment(DetailsFragmentFactory.EDIT_TASK, rowId);
		loadDetailFragment(detail);
	}
	
	public void loadDetailFragment(String type) {
		// should only be used by TabsFragment, which gets type names directly from the DetailsFragmentFactory
		DetailsFragment details = DetailsFragmentFactory.getDetailsFragment(type);
		loadDetailFragment(details);
	}
	
	/*
	 *  Puts the DetailsFragment object provided into details area 
	 */
	private void loadDetailFragment(DetailsFragment detail) {
		FragmentTransaction ft = getFragmentManager().beginTransaction();
		ft.replace(R.id.details, detail);
		ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
		ft.commit();
	}
	

}
